﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SosBL;

namespace SosForms
{
    public partial class FormSpeller : FormBase
    {

        #region " Constructores "

        /// <summary>
        /// Contructor
        /// </summary>
        public FormSpeller()
        {
            InitializeComponent();
        }

        public FormSpeller(string text, Utils.Language language)
        {
            InitializeComponent();
            lan = language;
            this.txtTexto.Text = text;

        }

        #endregion

        #region " Metodos "

        int _index = 0;
        string word = "";
        Utils.Language lan;

        /// <summary>
        /// check spell
        /// </summary>
        private void checkSpeller()
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            string[] words = this.txtTexto.Text.Split(delimiterChars);

            while (_index < words.Length)
            {
                word = words[_index];

                if (!Utils.isWordCorrect(word, lan))
                {
                    this.listSugerencias.Items.Clear();
                    remarcarPalabra(word);

                    foreach (string suggestion in Utils.GetSuggestionsSpeller(word, lan))
                    {
                        listSugerencias.Items.Add(suggestion);
                    }

                    break;
                }
                _index++;
            }
        }

        private void Cambiar()
        {
            string text = this.txtTexto.Text;
            this.txtTexto.Text = text.Replace(word, this.listSugerencias.SelectedItem.ToString());
            _index++;
            checkSpeller();

        }

        private void Omitir()
        {
            _index++;
            checkSpeller();
        }

        public string textOut = string.Empty;
        public bool cancelado = true;
        private void Aceptar()
        {
            cancelado = false;
            textOut = this.txtTexto.Text;
            this.Close();
        }

        private void Cancelar()
        {
            cancelado = true;
            this.Close();
        }

        private void remarcarPalabra(string word)
        {
            string originalText = txtTexto.Text;
            txtTexto.Text = "";
            txtTexto.Text = originalText;

            Random colorRandom = new Random();

            Color backColor = Color.FromArgb(colorRandom.Next(0, 255), colorRandom.Next(0, 255), colorRandom.Next(0, 255));

            int index = 0;

            while (index < this.txtTexto.Text.LastIndexOf(word))
            {
                this.txtTexto.Find(word, index, this.txtTexto.TextLength, RichTextBoxFinds.None);
                this.txtTexto.SelectionBackColor = backColor;
                this.txtTexto.SelectionColor = Utils.ContrastColor(backColor);
                index = txtTexto.Text.IndexOf(word, index) + 1;
            }
        }
        #endregion

        #region " Controles "

        /// <summary>
        /// Cambiar 
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butCambiar_Click(object sender, EventArgs e)
        {
            Cambiar();
        }

        /// <summary>
        /// Omitir
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butOmtir_Click(object sender, EventArgs e)
        {
            Omitir();
        }

        private void romButton3_Click(object sender, EventArgs e)
        {
            this.Aceptar();
        }

        private void romButton4_Click(object sender, EventArgs e)
        {
            this.Cancelar();
        }

        private void FormSpeller_Load(object sender, EventArgs e)
        {
            checkSpeller();
        }

        #endregion


    }
}
